/*
 * @Copyright: 2016 www.yyfax.com Inc. All rights reserved.
 */
package com.yyfax.pay.order.controller.api;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yyfax.framework.exception.YYException;
import com.yyfax.framework.model.YYFaxRsp;
import com.yyfax.framework.yyfax.enums.RspCodeEnum;
import com.yyfax.pay.common.annotation.CheckSign;
import com.yyfax.pay.common.enums.ApiTypeEnum;
import com.yyfax.pay.common.enums.PayModeEnum;
import com.yyfax.pay.common.enums.PayRspCodeEnum;
import com.yyfax.pay.common.enums.PayTypeEnum;
import com.yyfax.pay.common.enums.PaymentEnum;
import com.yyfax.pay.common.exception.YYPayException;
import com.yyfax.pay.common.model.to.YYPayApiReq2;
import com.yyfax.pay.order.model.to.bank.QueryBankQuotaListContent;
import com.yyfax.pay.order.model.to.bank.QueryBankQuotaParams;
import com.yyfax.pay.order.model.to.cardbin.CardInfoContent;
import com.yyfax.pay.order.model.to.cardbin.QueryCardBinApiDirectParams;
import com.yyfax.pay.order.model.to.route.RouteApiDirectContent;
import com.yyfax.pay.order.model.to.route.RouteApiDirectParams;
import com.yyfax.pay.order.service.PayCommonService;
import com.yyfax.pay.order.service.RouteService;
import com.yyfax.pay.third.model.to.yyfax.QueryCardBinContent;

/**
 * 支付公共外接接口
 * 
 * @author jiangmy
 * @date 2016-08-15 11:54:25
 * @since v1.0.0
 */
@RestController
public class PayCommonApiDirectController {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private PayCommonService payCommonService;

	@Autowired
	private RouteService routeSerivice;

	@CheckSign
	@RequestMapping(method = RequestMethod.POST, path = "/common/api/route/direct/query")
	public YYFaxRsp<RouteApiDirectContent> directRoute(@RequestBody @Valid YYPayApiReq2<RouteApiDirectParams> req) throws Exception {
		// if (req == null || req.getParams() == null) {
		// throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID);
		// }

		RouteApiDirectParams params = req.getParams();

		// 校验业务性
		params.validate();

		// // 2.验签
		// MerchantConfigPO merchantConfigPO = merchantConfigService.queryByMerchantCode(params.getMerchantCode());
		// if (merchantConfigPO == null) {
		// throw new YYException(PayRspCodeEnum.PAY_CODE_MERCHANT_NOT_EXISTS, "无效商户：" + params.getMerchantCode());
		// }
		//
		// req.checkSign(merchantConfigPO.getSignKey());

		QueryCardBinContent content = payCommonService.queryCardBin(req.getMerchantCode(), params.getCardNo());

		if (content == null) {
			logger.debug("查询卡bin返回null ");
			return new YYFaxRsp<RouteApiDirectContent>(PayRspCodeEnum.PAY_CODE_QUERY_CARD_BIN_ERROR, "银行卡查询异常", "查询卡bin异常");
		}

		PaymentEnum paymentEnum = null;

		if (PayTypeEnum.PAY.isValue(params.getPayType())) {// 如果是支付类型
			paymentEnum = routeSerivice.routePay(req.getMerchantCode(), content.getBankCode(), null, PayModeEnum.AUTH_PAY.getValue(),
					ApiTypeEnum.API.getValue());
		} else if (PayTypeEnum.AUTH.isValue(params.getPayType())) { // 如果是独立鉴权类型
			paymentEnum = routeSerivice.routeAuthentication(req.getMerchantCode(), null);
		} else {
			throw new YYPayException(PayRspCodeEnum.PAY_CODE_ILLEGAL_PAYTYPE);
		}

		if (paymentEnum == null) {
			throw new YYPayException(PayRspCodeEnum.COMMON_ERROR_OBJECT_NOT_FOUND, "没有找到相应路由信息", "没有找到相应路由信息");
		}

		RouteApiDirectContent routeApiDirectContent = new RouteApiDirectContent();
		routeApiDirectContent.setBankCode(content.getBankCode());
		routeApiDirectContent.setBankName(content.getBankName());
		routeApiDirectContent.setPaymentCode(paymentEnum.getValue());
		routeApiDirectContent.setPaymentName(paymentEnum.getRemark());

		return new YYFaxRsp<RouteApiDirectContent>(PayRspCodeEnum.SUCCESS, routeApiDirectContent);
	}

	/**
	 * 直连卡bin查询
	 * 
	 * @author buyi
	 * @date 2017-06-29 10:49:04
	 * @since v2.0.0
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@CheckSign
	@RequestMapping(method = RequestMethod.POST, path = "/api/common/card_bin/direct/query")
	public YYFaxRsp<CardInfoContent> queryCardBinDirect(@RequestBody @Valid YYPayApiReq2<QueryCardBinApiDirectParams> req) throws Exception {

		if (req == null || req.getParams() == null) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID);
		}

		QueryCardBinApiDirectParams params = req.getParams();

		QueryCardBinContent content = payCommonService.queryCardBin(req.getMerchantCode(), params.getCardNo());

		if (content == null) {
			logger.debug("查询卡bin返回null ");
			return new YYFaxRsp<CardInfoContent>(PayRspCodeEnum.PAY_CODE_QUERY_CARD_BIN_ERROR, "");
		}

		CardInfoContent cardInfoContent = new CardInfoContent(content.getBankCode(), content.getBankName(), content.getEnName());

		return new YYFaxRsp<CardInfoContent>(PayRspCodeEnum.fromValue(content.getResultCode()), cardInfoContent);

	}

	/**
	 * 查询银行限额信息
	 * 
	 * @author buyi
	 * @date 2017-09-13 10:49:57
	 * @since v2.0.0
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@CheckSign
	@RequestMapping(method = RequestMethod.POST, path = "/api/common/bank_quota/direct/query_list")
	public YYFaxRsp<QueryBankQuotaListContent> queryBankQuotaListForDirect(@RequestBody @Valid YYPayApiReq2<QueryBankQuotaParams> req)
			throws Exception {

		QueryBankQuotaListContent content = payCommonService.queryBankQuotaList2(req.getMerchantCode(), req.getParams());

		return new YYFaxRsp<QueryBankQuotaListContent>(PayRspCodeEnum.SUCCESS, content);
	}
}
